#include <bits/stdc++.h>
// 2025/01/19
// tag: 
// Author: Zhang Muen
using namespace std;

int n;
priority_queue<int, vector<int>, greater<int>> q;

signed main()
{
    int p;
    cin >> n;
    for (int i = 0; i < n; i++){
        cin >> p;
        q.push(p);
    }
    int ans = 0;
    while (q.size() > 1){
        int a = q.top();
        q.pop();
        int b = q.top();
        q.pop();
        ans += a + b;
        q.push(a + b);
    }
    cout << ans << endl;
    return 0;
}